# -*- coding: utf-8 -*-
import cherrypy
import logging
import functools
from hashlib import md5
import time
import datetime
import urllib2, urllib
import json
import random
import os

import config
import views

PWD_ERROR = {"code":203, "msg":"帐号/密码错误"}


def exp(method):
    @functools.wraps(method)
    def wrapper(*args, **kwargs):
        try:
            return method(*args, **kwargs)
        except:
            logging.error("api error: %s" % method.__name__, exc_info=True)
            return {"code":500, "msg":"程序错误,请稍等"}
    return wrapper

class Server:

    @cherrypy.expose
    @cherrypy.tools.json_out()
    def conf(self, cp, cv, bid, uid='', **kwargs):
        return views.conf(cp, cv, bid, uid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    def game_des(self, cp='', cv='', bid='', uid='', **kwargs):
        return views.game_des(cp, cv, bid, uid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    def user_info(self, cp='', cv='', bid='', uid='', **kwargs):
        return views.user_info(bid, uid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def login(self, cp, cv, bid, access_token='', openid='', devid='', login_type='wx', **kwargs):
        return views.login(cp, cv, bid, access_token, openid, devid, login_type)

    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def create_room(self, cp, cv, bid, uid='', pwd='', player='', game_type='gdmj', all_round=8, gps_lat='', gps_lon='', rule='', **kwargs):
        return views.create_room(cp, cv, bid, uid, pwd, player, game_type, all_round, gps_lat, gps_lon, rule)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def join_room(self, cp, cv, bid, uid='', pwd='', rid='', gps_lat='', gps_lon='', **kwargs):
        return views.join_room(cp, cv, bid, uid, pwd, rid, gps_lat, gps_lon)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def quit_room(self, cp, cv, bid, uid='', pwd='', rid='', **kwargs):
        return views.quit_room(cp, cv, bid, uid, pwd, rid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def dismiss_room(self, cp, cv, bid, uid='', pwd='', rid='', **kwargs):
        return views.dismiss_room(cp, cv, bid, uid, pwd, rid)
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    def upload(self, cp, cv, pwd='', bid='', uid='', up_file='', **kwargs):
        cherrypy.request.body.params
        name, ext = os.path.splitext(up_file.filename)
        today = time.strftime("%Y%m%d")
        save_path = "./voice/%s/%s/%s/%s" % (bid, today[:4], today[4:6], today[-2:])
        if not os.path.exists(save_path):
            os.makedirs(save_path)
        pic_tag = int(time.time())
        filename = "%s%s" % (pic_tag, ext.lower())
        file_path = "{path}/{file}".format(path=save_path, file=filename)
        f = open(file_path, 'wb+')
        f.write(up_file.file.read())
        f.close()
        if file_path[0] == '.':
            file_path = file_path[1:]
        url = "%s%s" % (config.UPLOAD_ADDR, file_path)
        return {"code":0, "msg":"ok", "data":{"url":url}}
    
    @cherrypy.expose
    @cherrypy.tools.json_out()
    @exp
    def feedback(self, bid, uid,content, **kwargs):
        return views.feedback(bid, uid, content)
    
    @cherrypy.expose
    def game_start(self, rid, bid='szjt', **kwargs):
        return views.game_start(rid, bid)
    
    
    